<?php

namespace app\admin\controller;
use Qiniu\Auth as Auth;
use Qiniu\Storage\UploadManager;
class Edition extends Common
{
    //版本列表
    public function edition_list()
    {
        $where = array('a.id' => array('gt', 0));
        $status = Request()->param('status');
        //状态
        $type = Request()->param('type');
        //类型
        $start_time = strtotime(Request()->param('start_time'));
        $end_time = strtotime(Request()->param('end_time'));
        $page = Request()->param('page');
        $size = Request()->param('size');
        if ($page < 1) {
            $page = 1;
        }
        if ($size < 15) {
            $size = 15;
        }
        $now = ($page - 1) * $size;
        if ($type != '') {
            $where['a.type'] = array('like', '%' . $type . '%');
        }
        if ($status > 0) {
            if ($status == 1) {
                $where['a.status'] = 1;
            } else {
                $where['a.status'] = 2;
            }

        }
        $where_time = array(array('gt', 0), array('gt', 0));
        if ($start_time > 0) {
            $where_time[] = array('egt', $start_time);
        } else {
            $start_time = '';
        }
        if ($end_time > 0) {
            $where_time[] = array('elt', $end_time);
        } else {
            $end_time = '';
        }
        $where['a.add_time'] = $where_time;
        $edition = db('edition a')
            ->where($where)
            ->limit("$now,$size")
            ->order('a.sort desc,a.status')
            ->select();
        //版本信息
        $count = db('edition a')
            ->where($where)
            ->count();
        //等级信息
        $page_str = page($page, $size, $count, count($edition));//分页
        $this->view->assign('page_str', $page_str);
        $this->view->assign('data', $edition);
        $this->assign('status', $status);
        $this->assign('type', $type);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('page', $page);
        $this->assign('size', $size);
        $this->assign('count', $count);
        return $this->view->fetch();
    }

    //添加版本
    function add_edition()
    {
        if (request()->isGet()) {
            return $this->view->fetch();
        } else if (request()->isAjax()) {
            $data = request()->post();
            $data['edition'] = trim($data['edition']);
            $data['remote_url'] = trim($data['remote_url']);
            if ($data['edition'] == '') {
                return return_json(array('code' => 0, 'msg' => '请输入版本号'), '添加版本');
            }
            if ($data['remote_url'] == '') {
                return return_json(array('code' => 0, 'msg' => '请输入远程下载地址'), '添加版本');
            }
            $edition = db('edition a')
                ->where(array('edition' => $data['edition'], 'type' => $data['type'],'status'=>1))
                ->find();
            if (!empty($edition)) {
                return return_json(array('code' => 0, 'msg' => '已有同版本的了'), '添加版本');
            }
            $file = Request()->file("download");
            if ($file) {
//                $info = $file->move(ROOT_PATH . 'public/upload/edition' . DS);
//                $file_str = "/upload/edition" . DS . $info->getSaveName();
                //以下为图片上传到其他地址(阿里云)
                $upload = new Upload();
                $return=$upload->upload($file);
                if($return['code']!=1){
                    return return_json(array('code'=>0,'msg'=>'文件上传失败:'.$return['msg']),'上传文件');
                }else{
                    $file_str=$return['url'];
                }
                //以上为图片上传到其他地址(阿里云)
                $data['down_url'] = $file_str;
            }
            $data['add_time'] = time();
            $data['admin_user_id'] = session('uid');
            $data['admin_user_name'] = session('user.real_name');
            unset($data['uploadfile']);
            $res=db('edition')->insertGetId($data);
            //添加权限
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'版本添加成功'),'添加版本');
            }else{
                return return_json(array('code'=>0,'msg'=>'版本添加失败'),'添加版本');
            }
        }
    }
    //禁用版本
    function edition_del(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $res=db('edition')
                ->where(array('id'=>$id))
                ->update(array('status'=>2,'update_time'=>time()));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'禁用成功'),'禁用版本');
            }else{
                return return_json(array('code'=>0,'msg'=>'禁用失败'),'禁用版本');
            }
        }
    }
    //启用版本
    function edition_use(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $edition=db('edition')
                ->where(array('id'=>$id))
                ->find();
            $edition1=db('edition')
                ->where(array('edition' => $edition['edition'], 'type' => $edition['type'],'status'=>1))
                ->find();
            if(!empty($edition1)){
                return return_json(array('code'=>0,'msg'=>'还有正在启用的相同版本'),'启用版本');
            }
            $res=db('edition')
                ->where(array('id'=>$id))
                ->update(array('status'=>1,'update_time'=>time()));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'启用成功'),'启用版本');
            }else{
                return return_json(array('code'=>0,'msg'=>'启用失败'),'启用版本');
            }
        }
    }
    //取消强制
    function force_del(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $res=db('edition')
                ->where(array('id'=>$id))
                ->update(array('is_force'=>2,'update_time'=>time()));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'取消强制成功'),'取消强制');
            }else{
                return return_json(array('code'=>0,'msg'=>'取消强制失败'),'取消强制');
            }
        }
    }
    //强制
    function force_use(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $res=db('edition')
                ->where(array('id'=>$id))
                ->update(array('is_force'=>1,'update_time'=>time()));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'强制成功'),'强制');
            }else{
                return return_json(array('code'=>0,'msg'=>'强制失败'),'强制');
            }
        }
    }
}